#include "historyWidget.h"
#include <QPushButton>
#include <QLabel>
#include <QWidget>
#include <QGridLayout>
#include <QMouseEvent>

//
#ifdef NOPREVIEW
historyWidget::historyWidget( const QString &title, const UploadResult &textResults )
#else
historyWidget::historyWidget( const QPixmap &preview, const QString &title, const UploadResult &textResults )
#endif
	: QWidget(),
	linkStructure(textResults)
{
	ui.setupUi(this);
#ifndef NOPREVIEW
	if (&preview != 0)
		ui.PreviewLabel->setPixmap(preview);
	else {
		QPixmap prw;
		ui.PreviewLabel->setPixmap(prw);
	}
#endif
	ui.fileNameLabel->setText(title);
	ui.textLabel->setText(textResults.Image.toString());
	select(); // need it?
	sl.setupUi(&showLinks);
//	linkStructure = textResults;
}

void historyWidget::mouseDoubleClickEvent(QMouseEvent *event) // open ImageLinks
{
	if ( event->button() != 1 )
		return;
#ifndef NOPREVIEW
	sl.PreviewLabel->setPixmap(*ui.PreviewLabel->pixmap());
#endif //NOPREVIEW
	sl.nameLabel->setText(linkStructure.ImageName);
	sl.htmlLabel->setText(linkStructure.ImageHtml.toString());
	sl.hotlinkLabel->setText(linkStructure.Image.toString());
	sl.thumbLabel->setText(linkStructure.Thumb.toString());
	sl.formattedLinks->setPlainText(resultText());
	showLinks.show();
}

QString historyWidget::resultText(bool html) // defaul false
{
	QString resTemplate = Config.value(html ? "resTemplateHtml" : "resTemplatePlain").toString();
	return resultText(linkStructure, resTemplate, html);
}

QString historyWidget::resultText(const UploadResult &links, QString resTemplate, const bool html)
{
	if ( ! links.success )
		return QString::null; // :-)))
	const QString tagHtml = html ? "<a href=\"%s\">%s</a>" : "%s";
	resTemplate.replace("%FILE%", QString(tagHtml).replace("%s", links.ImageName));
	resTemplate.replace("%HTML%", QString(tagHtml).replace("%s", links.ImageHtml.toString()));
	resTemplate.replace("%IMAGE%", QString(tagHtml).replace("%s", links.Image.toString()));
	resTemplate.replace("%THUMB%", QString(tagHtml).replace("%s", links.Thumb.toString()));
	resTemplate.replace("%SESSION_HTML%", QString(tagHtml).replace("%s", links.SessionHtml.toString()));
	return resTemplate;
}
//
